<?php
/**
 * 年龄检查
 * User: GRAY
 * Date: 16/8/11
 * Time: 下午10:23
 */

namespace Home\Common\Hook;


use Common\Library\BusinessException;
use Common\Library\Constant;
use Common\Vendor\Hook\BaseHook;
use Think\Log;


class HuluCheck extends BaseHook
{


    	private function insertHuluReport($token, $report_content){
    	    $sql = "SELECT * FROM  `qtq_dhb_report`  WHERE  customer_token= '".$token."'";
    	    $result = M()->query($sql);
    	    if($result && count($result)>0){
    	        return true;
    	    }else {
    	        $sql = "INSERT INTO `qtq_dhb_report`(`customer_token`, `report_content`, `creat_time`) VALUES ('".$token."','" . $report_content . "'," . time() . ")";
    	        $result = M()->execute($sql);
    	        return $result;
    	    }
    	}
   
	private function getHuluReport($token){
		$sql = "SELECT * FROM  `qtq_dhb_report`  WHERE  customer_token= '".$token."'";
		$result = M()->query($sql);
		if($result && count($result)>0){
			return $result[0]["report_content"];
		}

		//远程抓取所有信息
		$str_all = file_get_contents("http://api.huoyanzichan.com/Home/Hulushuju/getReport?token=".$token);
		$str_all = substr($str_all,strpos($str_all,'{'));
		$arr_data = array('customer_token' => $token, 'report_content' => $str_all);
		$this->insertHuluReport($token, $str_all);
		return $str_all;
	}

    public function execute($data, $params = [])
        {
		//获取token值
	    $invalid_code_list = [];
	    $customerId = $params["customerId"];
            $Model = D('Customer');
            $sql = "SELECT cd.trade_no FROM certificate_data cd  JOIN customer c ON c.real_name=cd.name and c.mobile=cd.mobile WHERE cd.data_type=2  AND c.id = ".$customerId." ORDER BY cd.id DESC LIMIT 1";
            $arr_id = $Model->query($sql);
            $token = $arr_id[0]['trade_no'];
            if(empty($token)){
		//TODO:
		BusinessException::throwException(Constant::INVALID_NO_OPERATOR);
	    }
            Log::write("通话token：".$token,Log::DEBUG);
            $behave = $arr = $contacts = $flag = $del = $del1 = $urgent = $list0 = array();

	    $str_all = $this->getHuluReport($token);
            $arr_all = json_decode($str_all);
            $hulu_data = $arr_all->data;
            $person = $hulu_data->person;//基本信息
            $zm_score = D('Customer')->where("id =$customerId")->getField('sesame_score');//芝麻分
            $behavior_check = $hulu_data->behavior_check;//申报信息核查
            if(count($behavior_check)>27) {
                $behave[] = $behavior_check[4];
                $behave[] = $behavior_check[5];
                $behave[] = $behavior_check[6];
                $behave[] = $behavior_check[7];
                $behave[] = $behavior_check[8];
                $behave[] = $behavior_check[9];
                $behave[] = $behavior_check[10];
                $behave[] = $behavior_check[2];
                $behave[] = $behavior_check[24];
                $behave[] = $behavior_check[25];
                $behave[] = $behavior_check[26];
                $behave[] = $behavior_check[27];
            }
            $application_check = $hulu_data->application_check;//申报信息核查--是否绑定身份证
            if(count($application_check)>15){
                $is_valid_idcard = $application_check[6]; //身份证是否有效
                $is_name_same_with_operator = $application_check[7]; //姓名与运营商是否匹配
                $is_idcard_same_with_operator = $application_check[8]; //身份证号是否运营商匹配
	        if(!($is_valid_idcard && $is_valid_idcar->score != 1) && $params["invalid_idcard_no_pass"]){
		    //BusinessException::throwException(Constant::INVALID_IDCARD);
	            $invalid_code_list[] = $Constant::INVALID_IDCARD;
		}

	        if(!($is_name_same_with_operator && $is_name_same_with_operator->score == 1) && $params["name_not_same_with_opeartor_no_pass"]){
		    //BusinessException::throwException(Constant::INVALID_NAME_NOT_SAME_WITH_OPERATOR);
	            $invalid_code_list[] = $Constant::INVALID_NAME_NOT_SAME_WITH_OPERATOR;
		}

	        if(!($is_idcard_same_with_operator && $is_idcard_same_with_operator->score == 1) && $params["idcard_not_same_with_opeartor_no_pass"]){
		    //BusinessException::throwException(Constant::INVALID_IDCARD_NOT_SAME_WITH_OPERATOR);
	            $invalid_code_list[] = $Constant::INVALID_IDCARD_NOT_SAME_WITH_OPERATOR;
		}
            }else{
		    BusinessException::throwException(Constant::INVALID_CHECK_INFO);
	    }

            $contact_list = $hulu_data->contact_list;//通讯详单
            foreach($contact_list as $arr2){
                $flag[]=$arr2->call_cnt;
            }


            $cell_behavior = $hulu_data->cell_behavior;//运营商账单
	    $cell_num  =0;
	    foreach ($cell_behavior as $k=>$v){
		    foreach ($v->behavior as $ke=>$ve){
			    if($ve->total_amount){
				    $cell_num ++;
			    }
		    }
	    }

	    if($cell_num < $params["min_bill_num"]){
		    //BusinessException::throwException(Constant::INVALID_BILL_NUM);
	            $invalid_code_list[] = $Constant::INVALID_BILL_NUM;
	    }
	   if(count($invalid_code_list) > 0){
		$data["invalid_code_list"] = $invalid_code_list;

	   }
            return $data;
        }
} 
